package cn.edu.djtu.fw.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.djtu.fw.dao.NewsMapper;
import cn.edu.djtu.fw.model.News;
import cn.edu.djtu.fw.service.NewsServiceI;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service("newsService")
public class NewsServiceImpl implements NewsServiceI {
	private NewsMapper newsMapper;

	public NewsMapper getNewsMapper() {
		return newsMapper;
	}

	@Autowired
	public void setNewsMapper(NewsMapper newsMapper) {
		this.newsMapper = newsMapper;
	}

	@Override
	public boolean delete(int nid) {
		try {
			newsMapper.deleteByPrimaryKey(nid);
			return true;
		} catch (Exception e) {
		}
		return false;
	}

	@Override
	public boolean update(News news) {
		try {
			newsMapper.updateByPrimaryKey(news);
			return true;
		} catch (Exception e) {
		}
		return false;
	}

	@Override
	public boolean insert(News news) {
		try {
			newsMapper.insert(news);
			return true;
		} catch (Exception e) {
		}
		return false;
	}

	@Override
	public News getNewsById(int nid) {
		return newsMapper.selectByPrimaryKey(nid);
	}

	@Override
	public Map<String, Object> getAllNews(int page, int rows, News news) {
		try {
			PageHelper.startPage(page, rows, true);
			List<News> newss = newsMapper.getAllNews(news);
			Map<String, Object> newsMap = new HashMap<String, Object>();
			newsMap.put("rows", newss);
			newsMap.put("total", ((Page<News>) newss).getTotal());
			return newsMap;
		} catch (Exception e) {
		}
		return null;
	}
}
